package com.cell.api.application.netty.utils;

import com.cell.api.application.netty.code.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 数据包code
 * </p>
 *
 * @author zdy
 * @date 2021/11/6
 */
@Slf4j
public class CoderUtils {


    /**
     * 数据帧的开始部分，由 0xAA，0xBB 组成两个字节帧头，标识数据帧的开始
     */
    public static final String DATA_FRAME1 = "0xAA";
    public static final String DATA_FRAME2 = "0xBB";
    /**
     * 十六进制长度
     */
    public static final int HEX_LENGTH = 2;

    /**
     * 十六进制字符串
     */
    public static final String HEX_STR = "0x";

    /**
     * 数据编码
     *
     * @param byteBuf 缓冲字节对象
     * @param packet  编码的对象
     */
    public static void encode(ByteBuf byteBuf, Packet packet) {
        byteBuf.writeByte(Integer.valueOf(DATA_FRAME1, 16));
        byteBuf.writeByte(Integer.valueOf(DATA_FRAME2, 16));
        byteBuf.writeByte(Integer.valueOf(packet.getGetCommand(), 16));
        byte[] content = packet.getBytes();
        int contentLenHex = content.length;
        byte[] contentLen = ByteUtils.getLowHighByte(contentLenHex);
        for (byte b : contentLen) {
            byteBuf.writeByte(b);
        }
        byteBuf.writeBytes(content);
        ByteBuf byteBufCopy = Unpooled.copiedBuffer(byteBuf);
        log.info("数据编码内容:" + ByteBufUtil.hexDump(byteBufCopy));
    }

    /**
     * 数据解码
     *
     * @param byteBuf 缓冲字节对象
     * @return 解码得到的对象
     */
    public static Packet decode(ByteBuf byteBuf) {
        ByteBuf byteBufCopy = Unpooled.copiedBuffer(byteBuf);
        log.info("数据解码内容:" + ByteBufUtil.hexDump(byteBufCopy));

        //跳过帧头
        byteBuf.skipBytes(2);
        //指令
        byte command = byteBuf.readByte();
        //数据长度
        byte[] bytes = new byte[2];
        byteBuf.readBytes(bytes);
        int lowHighByteInt = ByteUtils.getLowHighByteInt(bytes);
        log.info("数据解码长度:" + lowHighByteInt);
        //数据
        byte[] contentBytes = new byte[lowHighByteInt];
        byteBuf.readBytes(contentBytes);
        //指令十六进制
        StringBuilder sb = new StringBuilder();
        String inHex = Integer.toHexString(command & 0xFF);
        if (!inHex.contains(HEX_STR)) {
            sb.append(HEX_STR);
        }
        if (inHex.length() < HEX_LENGTH) {
            sb.append(0);
        }
        sb.append(inHex);
        Packet packet = new Packet();
        packet.setGetCommand(sb.toString());
        packet.setDataLength(lowHighByteInt);
        packet.setBytes(contentBytes);
        return packet;
    }


    public static void main(String[] args) {
//        int contentLenHex = Integer.parseInt(Integer.toHexString(256));
//        System.out.println(contentLenHex);
//        int lowHighByteInt = new BigInteger(contentLenHex + "", 16).intValue();
//        System.out.println(lowHighByteInt);

//        int contentLenHex = Integer.parseInt(Integer.toHexString(256));
//        System.out.println(contentLenHex);
        // byte[] contentLen = ByteUtils.getLowHighByte(256);
        // int lowHighByteInt = new BigInteger( + "", 16).intValue();
        //   System.out.println(ByteUtils.getLowHighByteInt(contentLen));
//        ByteBuf heapBuf = Unpooled.buffer(2);
//        heapBuf.writeByte(DATA_FRAME1);
//        heapBuf.writeByte(DATA_FRAME2);
//        heapBuf.writeByte(Command.TELEMETRY_SEND);
//        JSONObject json = new JSONObject();
//        json.put("content", "test");
//        byte[] content = json.toString().getBytes();
//        byte[] contentLen = ByteUtils.getLowHighByte(content.length);
//        for (byte b : contentLen) {
//            heapBuf.writeByte(b);
//        }
//        heapBuf.writeBytes(content);
//        while (heapBuf.isReadable()) {
//            int b = heapBuf.readByte() & 0xFF;    //读取1个字节转成10进制int表示
//            System.out.print(Integer.toHexString(b));    //再转成2个16进制字符串
//        }
        //跳过帧头
//        heapBuf.skipBytes(3);
//        byte[] bytes = new byte[2];
//        heapBuf.readBytes(bytes);
//        int lowHighByteInt = ByteUtils.getLowHighByteInt(bytes);
//        System.out.println(lowHighByteInt);
//        byte[] contentBytes = new byte[lowHighByteInt];
//        heapBuf.readBytes(contentBytes);
//        System.out.println(new String(contentBytes));

        byte[] bytes = ByteBufUtil.decodeHexDump("AABB01E6000300190002000600000000000300060000000000040006000000000005000600000000000600060000000000070006000000000001000600000000001400060000000000130006000000000012000600000000000E0006000000000019000600000000001A000600000000001B000600000000001C0006000000000000000600000000001800030000150006000000000016000300001100060000000000080006000000000009000600000000000A000600000000000B000600000000000C000600000000000D000600000000000F0006000000000017000600000000001000060000000000");
        ByteBuf byteBufCopy = Unpooled.copiedBuffer(bytes);
        Packet decode = decode(byteBufCopy);
    }
}
